# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 使用tfidfVectorizer实现.py
# @Author: dongguangwen
# @Date  : 2024-07-13 22:49
from sklearn.feature_extraction.text import TfidfVectorizer

# 示例文本
texts = [
    "Python is a programming language.",
    "Java is also a programming language.",
    "Python and Java are popular programming languages."
]

# 创建TF-IDF向量化器
vectorizer = TfidfVectorizer()

# 计算文本的TF-IDF值
tfidf_matrix = vectorizer.fit_transform(texts)

# 查看每个词语的IDF值
print("IDF scores:", vectorizer.idf_)

# 查看向量化后的文本数据（TF-IDF值）
print("TF-IDF matrix:\n", tfidf_matrix.toarray())

# 查看特征名称（词汇）
print("Feature names:", vectorizer.get_feature_names_out())

"""
IDF scores: [1.69314718 1.69314718 1.69314718 1.28768207 1.28768207 1.28768207
 1.69314718 1.69314718 1.         1.28768207]
TF-IDF matrix:
 [[0.         0.         0.         0.52682017 0.         0.52682017
  0.         0.         0.40912286 0.52682017]
 [0.56943086 0.         0.         0.43306685 0.43306685 0.43306685
  0.         0.         0.33631504 0.        ]
 [0.         0.4261835  0.4261835  0.         0.32412354 0.
  0.4261835  0.4261835  0.25171084 0.32412354]]
Feature names: ['also' 'and' 'are' 'is' 'java' 'language' 'languages' 'popular'
 'programming' 'python']
 """